package cn.enilu.flash.api.controller.park;

import cn.enilu.flash.bean.entity.park.ParkingQuote;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.service.park.ParkingQuoteService;

import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.dictmap.CommonDict;
import cn.enilu.flash.bean.enumeration.BizExceptionEnum;
import cn.enilu.flash.bean.exception.GunsException;
import cn.enilu.flash.bean.vo.front.Rets;

import cn.enilu.flash.utils.BeanUtil;
import cn.enilu.flash.utils.Maps;
import cn.enilu.flash.utils.ToolUtil;
import cn.enilu.flash.utils.factory.Page;

import cn.enilu.flash.warpper.ParkQuoteWarpper;
import cn.enilu.flash.warpper.RoomInfoWarpper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/quote")
public class ParkingQuoteController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private ParkingQuoteService parkingQuoteService;

	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public Object list(@RequestParam(required = false) String communityId,
					   @RequestParam(required = false) String quoteType) {
		Page<ParkingQuote> page = new PageFactory<ParkingQuote>().defaultPage();
		page.addFilter("communityId", SearchFilter.Operator.EQ, communityId);
		page.addFilter("quoteType", SearchFilter.Operator.EQ, quoteType);
		page.setSort(new Sort(Sort.Direction.ASC, "communityId", "quoteType", "quoteUnit", "quoteRange"));
		page = parkingQuoteService.queryPage(page);
		List list = (List) new ParkQuoteWarpper(BeanUtil.objectsToMaps(page.getRecords())).warp();
		page.setRecords(list);
		return Rets.success(page);
	}
	@RequestMapping(method = RequestMethod.POST)
	@BussinessLog(value = "编辑车位报价", key = "name",dict= CommonDict.class)
	public Object save(@ModelAttribute ParkingQuote parkingQuote){
		if(parkingQuote.getId()==null){
			parkingQuoteService.insert(parkingQuote);
		}else {
			parkingQuoteService.update(parkingQuote);
		}
		return Rets.success();
	}
	@RequestMapping(method = RequestMethod.DELETE)
	@BussinessLog(value = "删除车位报价", key = "id",dict= CommonDict.class)
	public Object remove(Long id){
		if (ToolUtil.isEmpty(id)) {
			throw new GunsException(BizExceptionEnum.REQUEST_NULL);
		}
		parkingQuoteService.delete(id);
		return Rets.success();
	}

	@RequestMapping(value = "/getParkingQuotes/{parkingId}", method = RequestMethod.GET)
	public Object getParkingQuotes(@PathVariable("parkingId") Long parkingId){
		if (ToolUtil.isEmpty(parkingId)) {
			throw new GunsException(BizExceptionEnum.REQUEST_NULL);
		}
		List<ParkingQuote> list = parkingQuoteService.getParkingQuotes(parkingId);
		return Rets.success(new ParkQuoteWarpper(BeanUtil.objectsToMaps(list)).warp());
	}
}